package com.ruoyi.cderp.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.cderp.domain.TblReceiveSheetDetail;
import com.ruoyi.cderp.mapper.TblProductStockMapper;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.cderp.domain.TblSaleOutSheetDetail;
import com.ruoyi.cderp.mapper.TblSaleOutSheetMapper;
import com.ruoyi.cderp.domain.TblSaleOutSheet;
import com.ruoyi.cderp.service.ITblSaleOutSheetService;

/**
 * 销售出库单Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-26
 */
@Service
public class TblSaleOutSheetServiceImpl implements ITblSaleOutSheetService
{
    // 存储当前登录用户对象
    LoginUser loginUser = null;
    @Autowired
    private TblSaleOutSheetMapper tblSaleOutSheetMapper;
    @Autowired
    private TblProductStockMapper tblProductStockMapper;

    /**
     * 查询销售出库单
     *
     * @param id 销售出库单主键
     * @return 销售出库单
     */
    @Override
    public TblSaleOutSheet selectTblSaleOutSheetById(String id)
    {
        return tblSaleOutSheetMapper.selectTblSaleOutSheetById(id);
    }

    /**
     * 查询销售出库单列表
     *
     * @param tblSaleOutSheet 销售出库单
     * @return 销售出库单
     */
    @Override
    public List<TblSaleOutSheet> selectTblSaleOutSheetList(TblSaleOutSheet tblSaleOutSheet)
    {
        // 获取当前登录用户对象
        loginUser = SecurityUtils.getLoginUser();

        return tblSaleOutSheetMapper.selectTblSaleOutSheetList(tblSaleOutSheet);
    }

    /**
     * 新增销售出库单
     *
     * @param tblSaleOutSheet 销售出库单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTblSaleOutSheet(TblSaleOutSheet tblSaleOutSheet)
    {
        tblSaleOutSheet.setCreateTime(DateUtils.getNowDate());

        tblSaleOutSheet.setCreateById(loginUser.getUser().getUserId().toString());
        tblSaleOutSheet.setCreateBy(loginUser.getUser().getUserName());

        int rows = tblSaleOutSheetMapper.insertTblSaleOutSheet(tblSaleOutSheet);
        insertTblSaleOutSheetDetail(tblSaleOutSheet);
        return rows;
    }

    /**
     * 修改销售出库单
     *
     * @param tblSaleOutSheet 销售出库单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTblSaleOutSheet(TblSaleOutSheet tblSaleOutSheet)
    {
        if(tblSaleOutSheet.getStatus()==3){
            for (TblSaleOutSheetDetail tblSaleOutSheetDetail : tblSaleOutSheet.getTblSaleOutSheetDetailList()) {
                //库存数量
                long stockNum = tblSaleOutSheetDetail.getBaseDataProduct().getTblProductStock().getStockNum()-tblSaleOutSheetDetail.getOrderNum();
                tblSaleOutSheetDetail.getBaseDataProduct().getTblProductStock().setStockNum(stockNum);
                //库存数量的含税总金额
                BigDecimal taxAmount = BigDecimal.valueOf(stockNum).multiply(tblSaleOutSheetDetail.getBaseDataProduct().getTblProductStock().getTaxPrice());
                tblSaleOutSheetDetail.getBaseDataProduct().getTblProductStock().setTaxAmount(taxAmount);
                //修改商品库存
                tblProductStockMapper.updateTblProductStock(tblSaleOutSheetDetail.getBaseDataProduct().getTblProductStock());
            }
        }
        tblSaleOutSheet.setUpdateTime(DateUtils.getNowDate());

        tblSaleOutSheet.setUpdateById(loginUser.getUser().getUserId().toString());
        tblSaleOutSheet.setUpdateBy(loginUser.getUser().getUserName());
        if (tblSaleOutSheet.getApproveBy()!=null){
            tblSaleOutSheet.setApproveBy((loginUser.getUser().getUserName()));
        }

        tblSaleOutSheetMapper.deleteTblSaleOutSheetDetailBySheetId(tblSaleOutSheet.getId());
        insertTblSaleOutSheetDetail(tblSaleOutSheet);
        return tblSaleOutSheetMapper.updateTblSaleOutSheet(tblSaleOutSheet);
    }

    /**
     * 批量删除销售出库单
     *
     * @param ids 需要删除的销售出库单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTblSaleOutSheetByIds(String[] ids)
    {
        tblSaleOutSheetMapper.deleteTblSaleOutSheetDetailBySheetIds(ids);
        return tblSaleOutSheetMapper.deleteTblSaleOutSheetByIds(ids);
    }

    /**
     * 删除销售出库单信息
     *
     * @param id 销售出库单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTblSaleOutSheetById(String id)
    {
        tblSaleOutSheetMapper.deleteTblSaleOutSheetDetailBySheetId(id);
        return tblSaleOutSheetMapper.deleteTblSaleOutSheetById(id);
    }

    /**
     * 新增销售出库单信息
     *
     * @param tblSaleOutSheet 销售出库单对象
     */
    public void insertTblSaleOutSheetDetail(TblSaleOutSheet tblSaleOutSheet)
    {
        List<TblSaleOutSheetDetail> tblSaleOutSheetDetailList = tblSaleOutSheet.getTblSaleOutSheetDetailList();
        String id = tblSaleOutSheet.getId();
        if (StringUtils.isNotNull(tblSaleOutSheetDetailList))
        {
            List<TblSaleOutSheetDetail> list = new ArrayList<TblSaleOutSheetDetail>();
            for (TblSaleOutSheetDetail tblSaleOutSheetDetail : tblSaleOutSheetDetailList)
            {
                tblSaleOutSheetDetail.setSheetId(id);
                list.add(tblSaleOutSheetDetail);
            }
            if (list.size() > 0)
            {
                tblSaleOutSheetMapper.batchTblSaleOutSheetDetail(list);
            }
        }
    }
}
